/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spwpanel;

import context.Client;
import hibernate.data.Groups;
import hibernate.data.Projects;
import hibernate.data.Users;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author konp
 */
public class GroupsWindow extends javax.swing.JFrame {
    
    
    private Client client = null;
    
    private Groups group = null;

    public void setClient(Client client) {
        this.client = client;
    }
    
    

    /**
     * Creates new form GroupsWindow
     */
    public GroupsWindow(Client client) {
        initComponents();
        
        this.client = client;
        
        List projectList = client.getSession().createCriteria(Projects.class).list();
        List usersList = client.getSession().createCriteria(Users.class).list();
        
        ArrayList managerList = new ArrayList();
        if(usersList != null && usersList.size() > 0){
            for (int i =0; i < usersList.size(); i++){
                Users user = (Users) usersList.get(i);
                if (user.getUrsKind().getUksName().equalsIgnoreCase(
                    "Project Manager")){
                    managerList.add(user);
                }
            }
        }
        
        for (int i = 0; i < projectList.size(); i++){
            Projects proj = (Projects)projectList.get(i);
            this.projectGroupsWindow.addItem(proj.getPrjName());
        }
        
        
        for (int i = 0; i < managerList.size(); i++){
            Users manager = (Users)managerList.get(i);
            this.managerGroupsWindow.addItem(manager.getUrsLastName() + " " + manager.getUrsFirstName());
        }

        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameGroupsField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        createGroupButton = new javax.swing.JButton();
        cancelGroupsButton = new javax.swing.JButton();
        projectGroupsWindow = new javax.swing.JComboBox();
        managerGroupsWindow = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nazwa :");

        nameGroupsField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameGroupsFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("Projekt :");

        jLabel4.setText("Manager :");

        createGroupButton.setText("Utwórz Grupę");
        createGroupButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createGroupButtonActionPerformed(evt);
            }
        });

        cancelGroupsButton.setText("Anuluj");
        cancelGroupsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelGroupsButtonActionPerformed(evt);
            }
        });

        jLabel3.setForeground(new java.awt.Color(204, 0, 51));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(nameGroupsField)
                                    .addComponent(createGroupButton, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                                    .addComponent(managerGroupsWindow, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(projectGroupsWindow, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cancelGroupsButton)
                        .addGap(110, 110, 110))))
            .addGroup(layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameGroupsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectGroupsWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(managerGroupsWindow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addComponent(jLabel3)
                .addGap(39, 39, 39)
                .addComponent(createGroupButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelGroupsButton)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameGroupsFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameGroupsFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameGroupsFieldActionPerformed

    private void cancelGroupsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelGroupsButtonActionPerformed
        // TODO add your handling code here:
        setVisible(false);
    }//GEN-LAST:event_cancelGroupsButtonActionPerformed

    private void createGroupButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createGroupButtonActionPerformed
        // TODO add your handling code here:
        String groupsName = nameGroupsField.getText();
        
        if(groupsName == null || groupsName.equalsIgnoreCase("")){
            jLabel3.setText("Podaj nazwę!");
            nameGroupsField.setBackground(Color.red);
            
            
            return;
        }
        Session ses = client.getSession();
        List groupList = ses.createCriteria(Groups.class).add(Restrictions.eq("grpName", groupsName)).list();
        if (groupList != null && groupList.size() > 0){
            jLabel3.setText("Grupa o podanej nazwie już istnieje!");
            nameGroupsField.setBackground(Color.red);
            return;
        }
        
        
        
        List managers = ses.createCriteria(Users.class).list();
        Users manager = null;
        for (int i = 0; i < managers.size(); i++){
            Users user = (Users) managers.get(i);
            if (user.getUrsKind().getUksName().equalsIgnoreCase("Project Manager") &&
                    (user.getUrsLastName() + " " + user.getUrsFirstName()).equalsIgnoreCase((String)managerGroupsWindow.getSelectedItem())){
                manager = user;
                break;
                
            }
        }
        
        
        List proj = ses.createCriteria(Projects.class).add(Restrictions.eq("prjName", (String)projectGroupsWindow.getSelectedItem())).list();
        Projects project = (Projects) proj.get(0);
        
        if(this.group != null){
            this.group.setGrpManager(manager);
            this.group.setGrpName(groupsName);
            this.group.setGrpProject(project);
            ses.update(this.group);
            setVisible(false);
            return;
        }
        
        Date date = new Date();
        Groups group = new Groups(date, groupsName, manager, project);
        ses.save(group);
        setVisible(false);
        
    }//GEN-LAST:event_createGroupButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GroupsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GroupsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GroupsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GroupsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new GroupsWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelGroupsButton;
    private javax.swing.JButton createGroupButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JComboBox managerGroupsWindow;
    private javax.swing.JTextField nameGroupsField;
    private javax.swing.JComboBox projectGroupsWindow;
    // End of variables declaration//GEN-END:variables
}
